package cn.csc.module.product.service.attr;

import cn.csc.framework.common.exception.ServiceException;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.product.constant.AttrType;
import cn.csc.module.product.controller.admin.attr.vo.AttrCreateReqVO;
import cn.csc.module.product.controller.admin.attr.vo.AttrExportReqVO;
import cn.csc.module.product.controller.admin.attr.vo.AttrPageReqVO;
import cn.csc.module.product.controller.admin.attr.vo.AttrUpdateReqVO;
import cn.csc.module.product.convert.attr.AttrConvert;
import cn.csc.module.product.dal.dataobject.attr.AttrDO;
import cn.csc.module.product.dal.mysql.attr.AttrMapper;
import cn.csc.module.product.service.attrcategory.AttrCategoryService;
import cn.csc.module.product.service.attrvalue.AttrValueService;
import cn.csc.module.product.service.category.CategoryService;
import cn.csc.module.product.service.spuattr.SpuAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.product.enums.ErrorCodeConstants.ATTR_NOT_EXISTS;

/**
 * 属性信息 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class AttrServiceImpl implements AttrService {

    @Resource
    private AttrMapper attrMapper;

    @Autowired
    private AttrCategoryService attrCategoryService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SpuAttrValueService spuAttrValueService;

    @Override
    public BigInteger createAttr(AttrCreateReqVO createReqVO) {
        // 插入
        AttrDO attr = AttrConvert.INSTANCE.convert(createReqVO);
        attrMapper.insert(attr);
        // 返回
        return attr.getId();
    }

    @Override
    public void updateAttr(AttrUpdateReqVO updateReqVO) {
        // 校验存在
        validateAttrExists(updateReqVO.getId());
        // 更新
        AttrDO updateObj = AttrConvert.INSTANCE.convert(updateReqVO);
        attrMapper.updateById(updateObj);
    }

    @Override
    public void deleteAttr(BigInteger id) {
        // 校验存在
        validateAttrExists(id);
        // 删除
        attrMapper.deleteById(id);
    }

    private void validateAttrExists(BigInteger id) {
        if (attrMapper.selectById(id) == null) {
            throw exception(ATTR_NOT_EXISTS);
        }
    }

    @Override
    public AttrDO getAttr(BigInteger id) {
        return attrMapper.selectById(id);
    }

    @Override
    public AttrDO getByAttrId(BigInteger attrId) {
        AttrDO attrDO = attrMapper.getByAttrId(attrId);
        if (Objects.isNull(attrDO)) {
            throw new ServiceException(ATTR_NOT_EXISTS);
        }
        if (Objects.equals(attrDO.getAttrType(), AttrType.BASIC.value())) {
            attrDO.setCategories(attrCategoryService.listByAttrId(attrId));
            categoryService.getPathNames(attrDO.getCategories());
        }
        return attrDO;
    }

    @Override
    public List<AttrDO> getAttrList(Collection<BigInteger> ids) {
        return attrMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AttrDO> getAttrPage(AttrPageReqVO pageReqVO) {
        return attrMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AttrDO> getAttrList(AttrExportReqVO exportReqVO) {
        return attrMapper.selectList(exportReqVO);
    }

}
